﻿/****************************************************************
*   作者：Morain
*   创建时间：2018/2/23 19:35:44
*   描述说明：
*****************************************************************/
using Model;
using System;

namespace Hotfix
{
  public class OuterMessageDispatcher : IMessageDispatcher
  {
    public void Dispatch(Session session, PacketInfo packetInfo)
    {
      Type type = Game.Scene.GetComponent<OpcodeTypeComponent>().GetType(packetInfo.Opcode);
      IMessage message = (IMessage)session.Network.MessagePacker.DeserializeFrom(type, packetInfo.Bytes, packetInfo.Index, packetInfo.Length);

      if (message != null)
      {
        Game.Scene.GetComponent<MessageDispatcherComponent>().Handle(session, packetInfo.RpcId, message);
      }
      else
      {
        throw new Exception($"message type error: {message.GetType().FullName}");
      }
    }
  }
}